package net.adst.ui.other;

import com.toedter.calendar.JDateChooser;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.searcher.Searcher;
import net.adst.util.DataManager;
import net.adst.util.GBC;
import net.adst.util.MainDataBuilder;
import net.adst.util.MyUtils;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import sun.net.ResourceManager;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TreeSet;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 22.12.12
 * Time: 2:31
 * To change this template use File | Settings | File Templates.
 */
public class AddNaemPanel extends JPanel {
    private Dimension fieldDimension = new Dimension(125, 20);
    private JButton addButton;
    private JButton saveButton;
    private JButton clearButton;
    private JTable addingTable;
    private DefaultTableModel model;
    private JDateChooser dateTo;
    private JDateChooser dateFrom;
    private JTextField chInField;
    private JTextField chOutField;
    private JComboBox tecNameCombo;
    private JComboBox managerCombo;
    private JComboBox customerCombo;
    private JComboBox performerCombo;
    private JTextField priceHoursCustomerField;
    private JTextField priceHoursOurField;
    private JComboBox objectCombo;
    private JButton removeButton;

    public AddNaemPanel() {
        addElements();
        addTable();
        addLogic();
        setBackground(Constants.COLOR_LIGHT_BLUE);
    }

    public void addElements() {
        setLayout(new GridBagLayout());
        JLabel dateFromLabel = new JLabel("день с:");
        JLabel dateToLabel = new JLabel("день по:");
        dateFrom = new JDateChooser();
        Calendar dtFrom = Calendar.getInstance(), dtTo = Calendar.getInstance();
        dtFrom.set(Calendar.DAY_OF_WEEK, dtFrom.getFirstDayOfWeek());
        dateTo = new JDateChooser();
        dateFrom.setDate(dtFrom.getTime());
        dateTo.setDate(dtTo.getTime());
        dateTo.setMaximumSize(fieldDimension);
        dateTo.setPreferredSize(fieldDimension);
        dateTo.setMinimumSize(fieldDimension);
        dateFrom.setMaximumSize(fieldDimension);
        dateFrom.setPreferredSize(fieldDimension);
        dateFrom.setMinimumSize(fieldDimension);


        JLabel chInLabel = new JLabel("счет входящий:");
        chInField = new JTextField();
        chInField.setMaximumSize(fieldDimension);
        chInField.setPreferredSize(fieldDimension);
        chInField.setMinimumSize(fieldDimension);

        JLabel chOutLabel = new JLabel("счет исходящий:");
        chOutField = new JTextField();
        chOutField.setMaximumSize(fieldDimension);
        chOutField.setPreferredSize(fieldDimension);
        chOutField.setMinimumSize(fieldDimension);


        JLabel tecNameLabel = new JLabel("техника:");
        tecNameCombo = new JComboBox();
        DefaultComboBoxModel tecNameModel = new DefaultComboBoxModel();
        ArrayList<String> tecNameList = (ArrayList<String>) DataManager.getObject(VariableNames.TEC_NAEM_NAME_LIST);
        for (String item : tecNameList) {
            tecNameModel.addElement(item);
        }
        tecNameCombo.setModel(tecNameModel);
        tecNameCombo.setEditable(true);
        tecNameCombo.setMaximumSize(fieldDimension);
        tecNameCombo.setPreferredSize(fieldDimension);
        tecNameCombo.setMinimumSize(fieldDimension);

        JLabel managerLabel = new JLabel("менеджер:");
        managerCombo = new JComboBox();
        ArrayList<String> managerList = (ArrayList<String>) DataManager.getObject(VariableNames.NAEM_MENAGER_LIST);
        DefaultComboBoxModel managerModel = new DefaultComboBoxModel();
        for (String item : managerList) {
            managerModel.addElement(item);
        }
        managerCombo.setModel(managerModel);
        managerCombo.setEditable(true);
        managerCombo.setAutoscrolls(true);
        managerCombo.setMaximumSize(fieldDimension);
        managerCombo.setPreferredSize(fieldDimension);
        managerCombo.setMinimumSize(fieldDimension);

        JLabel customerLabel = new JLabel("заказчик:");
        customerCombo = new JComboBox();
        ArrayList<String> customerList = (ArrayList<String>) DataManager.getObject(VariableNames.TEC_NAEM_CUSTOMER_LIST);
        DefaultComboBoxModel customerModel = new DefaultComboBoxModel();
        for (String item : customerList) {
            customerModel.addElement(item);
        }
        customerCombo.setModel(customerModel);
        customerCombo.setEditable(true);
        customerCombo.setMaximumSize(fieldDimension);
        customerCombo.setPreferredSize(fieldDimension);
        customerCombo.setMinimumSize(fieldDimension);

        JLabel performerLabel = new JLabel("исполнитель:");
        performerCombo = new JComboBox();
        TreeSet<String> performerList = (TreeSet<String>) DataManager.getObject(VariableNames.TEC_NAEM_PERFORMER_LIST);
        DefaultComboBoxModel performerModel = new DefaultComboBoxModel();
        for (String item : performerList) {
            performerModel.addElement(item);
        }
        performerCombo.setModel(performerModel);
        performerCombo.setEditable(true);
        performerCombo.setMaximumSize(fieldDimension);
        performerCombo.setPreferredSize(fieldDimension);
        performerCombo.setMinimumSize(fieldDimension);


        JLabel priceHoursCustomerLabel = new JLabel("почем(час) исх:");
//        priceHoursCustomerLabel.setMaximumSize(labelDimension);
//        priceHoursCustomerLabel.setPreferredSize(labelDimension);
//        priceHoursCustomerLabel.setMinimumSize(labelDimension);
        priceHoursCustomerField = new JTextField();
        priceHoursCustomerField.setMaximumSize(fieldDimension);
        priceHoursCustomerField.setPreferredSize(fieldDimension);
        priceHoursCustomerField.setMinimumSize(fieldDimension);


        JLabel priceHoursOurLabel = new JLabel("почем(час) вх:");
//        priceHoursOurLabel.setMaximumSize(labelDimension);
//        priceHoursOurLabel.setPreferredSize(labelDimension);
//        priceHoursOurLabel.setMinimumSize(labelDimension);
        priceHoursOurField = new JTextField();
        priceHoursOurField.setMaximumSize(fieldDimension);
        priceHoursOurField.setPreferredSize(fieldDimension);
        priceHoursOurField.setMinimumSize(fieldDimension);

        JLabel objectNameLabel = new JLabel("объект:");
        objectCombo = new JComboBox();
        TreeSet<String> objectList = (TreeSet<String>) DataManager.getObject(VariableNames.TEC_NAEM_OBJECT_LIST);
        DefaultComboBoxModel objectModel = new DefaultComboBoxModel();
        for (String item : objectList) {
            objectModel.addElement(item);
        }
        objectCombo.setModel(objectModel);
        objectCombo.setEditable(true);
        objectCombo.setMaximumSize(fieldDimension);
        objectCombo.setPreferredSize(fieldDimension);
        objectCombo.setMinimumSize(fieldDimension);

        addButton = new JButton("добавить");
        addButton.setFocusPainted(false);
        clearButton = new JButton("удалить все записи.");
        saveButton = new JButton(new ImageIcon(ResourceManager.class.getResource("/images/save.png")));
        saveButton.setEnabled(false);

        removeButton = new JButton(new ImageIcon(ResourceManager.class.getResource("/images/remove.png")));
        removeButton.setEnabled(false);
        removeButton.setFocusPainted(false);
        removeButton.setToolTipText("Удалить запись(си) из таблицы.");

        JPanel addPanel = new JPanel();
        addPanel.setLayout(new GridBagLayout());
        addPanel.add(dateFromLabel, new GBC(0, 0).setInsets(4, 4).setAnchor(GBC.WEST));
        addPanel.add(dateFrom, new GBC(1, 0).setInsets(4, 4).setAnchor(GBC.WEST));
        addPanel.add(dateToLabel, new GBC(0, 1).setInsets(4, 4).setAnchor(GBC.WEST));
        addPanel.add(dateTo, new GBC(1, 1).setInsets(4, 4).setAnchor(GBC.WEST));

        addPanel.add(chInLabel, new GBC(0, 2).setInsets(8, 4, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(chInField, new GBC(1, 2).setInsets(8, 4).setAnchor(GBC.WEST));
        addPanel.add(chOutLabel, new GBC(0, 3).setInsets(4, 4, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(chOutField, new GBC(1, 3).setInsets(4, 4).setAnchor(GBC.WEST));

        addPanel.add(tecNameLabel, new GBC(4, 0).setInsets(4, 8, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(tecNameCombo, new GBC(5, 0).setInsets(4, 4).setAnchor(GBC.WEST));
        addPanel.add(managerLabel, new GBC(4, 1).setInsets(4, 8, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(managerCombo, new GBC(5, 1).setInsets(4, 4).setAnchor(GBC.WEST));

        addPanel.add(customerLabel, new GBC(4, 2).setInsets(8, 8, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(customerCombo, new GBC(5, 2).setInsets(8, 4).setAnchor(GBC.WEST));
        addPanel.add(performerLabel, new GBC(4, 3).setInsets(4, 8, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(performerCombo, new GBC(5, 3).setInsets(4, 4).setAnchor(GBC.WEST));


        addPanel.add(priceHoursCustomerLabel, new GBC(8, 0).setInsets(4, 8, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(priceHoursCustomerField, new GBC(9, 0).setInsets(4, 4).setAnchor(GBC.WEST));
        addPanel.add(priceHoursOurLabel, new GBC(8, 1).setInsets(4, 8, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(priceHoursOurField, new GBC(9, 1).setInsets(4, 4).setAnchor(GBC.WEST));

        addPanel.add(objectNameLabel, new GBC(8, 2).setInsets(8, 8, 4, 0).setAnchor(GBC.WEST));
        addPanel.add(objectCombo, new GBC(9, 2).setInsets(8, 4).setAnchor(GBC.WEST));

        addPanel.add(clearButton, new GBC(9, 3, 2, 1).setInsets(4, 4).setAnchor(GBC.WEST));
        addPanel.add(addButton, new GBC(10, 0).setInsets(4, 4));
        addPanel.add(saveButton, new GBC(10, 1, 1, 2).setInsets(4, 4));
        addPanel.add(removeButton, new GBC(10, 2, 1, 2).setInsets(24, 4, 4, 4));
        addPanel.setBackground(Constants.COLOR_LIGHT_BLUE);
        add(addPanel, new GBC(0, 0).setFill(GBC.BOTH).setWeight(1, 1));
    }

    private void addTable() {
        model = new NaemPanelDefaultTableModel( //NotEditableDefaultTableModel
                new Object[][]{
                },
                Constants.NAEM_НEADER
        );
        addingTable = new JTable(model);

        addingTable.setRowHeight(Constants.TABLE_ROW_HEIGHT);
        addingTable.getTableHeader().setReorderingAllowed(false);
        setWidth(addingTable);
        addingTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        JScrollPane scrollPane = new JScrollPane(addingTable);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        add(scrollPane, new GBC(0, 4).setFill(GBC.BOTH).setWeight(1, 1));

    }

    private void addLogic() {
        addingTable.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("tableCellEditor") && evt.getNewValue() != null) {
                    saveButton.setEnabled(false);
                }
                if (evt.getPropertyName().equals("tableCellEditor") && evt.getNewValue() == null) {
                    saveButton.setEnabled(true);
                }
            }
        });

        addingTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    removeButton.setEnabled(false);
                } else {
                    removeButton.setEnabled(true);
                }
            }
        });

        model.addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                int row = e.getFirstRow();
                if (e.getColumn() == 6 && !addingTable.getValueAt(row, 6).toString().isEmpty() && model.getRowCount() > 0) {
                    try {
                        Double price = Double.valueOf(addingTable.getValueAt(row, 6).toString());
                        if (!addingTable.getValueAt(row, 9).toString().isEmpty()) {
                            addingTable.setValueAt(Double.valueOf(addingTable.getValueAt(row, 9).toString()) * price, row, 10);
                        }
                    } catch (IllegalArgumentException ex) {
                        addingTable.setValueAt("", row, 6);
                    }

                }
                if (e.getColumn() == 9 && !addingTable.getValueAt(row, 9).toString().isEmpty() && model.getRowCount() > 0) {
                    try {
                        Double hours = Double.valueOf(addingTable.getValueAt(row, 9).toString());
                        if (!addingTable.getValueAt(row, 6).toString().isEmpty()) {
                            addingTable.setValueAt(Double.valueOf(addingTable.getValueAt(row, 6).toString()) * hours, row, 10);
                        }
                    } catch (IllegalArgumentException ex) {
                        addingTable.setValueAt("", row, 9);
                    }

                }

                if (e.getColumn() == 12 && !addingTable.getValueAt(row, 12).toString().isEmpty() && model.getRowCount() > 0) {
                    try {
                        Double price = Double.valueOf(addingTable.getValueAt(row, 12).toString());
                        if (!addingTable.getValueAt(row, 13).toString().isEmpty()) {
                            addingTable.setValueAt(Double.valueOf(addingTable.getValueAt(row, 13).toString()) * price, row, 14);
                        }
                    } catch (IllegalArgumentException ex) {
                        addingTable.setValueAt("", row, 12);
                    }

                }
                if (e.getColumn() == 13 && !addingTable.getValueAt(row, 13).toString().isEmpty() && model.getRowCount() > 0) {
                    try {
                        Double hours = Double.valueOf(addingTable.getValueAt(row, 13).toString());
                        if (!addingTable.getValueAt(row, 12).toString().isEmpty()) {
                            addingTable.setValueAt(Double.valueOf(addingTable.getValueAt(row, 12).toString()) * hours, row, 14);
                        }
                    } catch (IllegalArgumentException ex) {
                        addingTable.setValueAt("", row, 13);
                    }

                }

                if (e.getColumn() == 17) {
                    try {
                        Double arenda = !addingTable.getValueAt(row, 17).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 17).toString())) : new Double("0");
                    } catch (IllegalArgumentException ex) {
                        addingTable.setValueAt("", row, 17);
                    }

                }

                if (e.getColumn() == 18) {
                    try {
                        Double prorab = !addingTable.getValueAt(row, 18).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 18).toString())) : new Double("0");
                    } catch (IllegalArgumentException ex) {
                        addingTable.setValueAt("", row, 18);
                    }

                }

                if (e.getColumn() == 6 || e.getColumn() == 9 || e.getColumn() == 12 || e.getColumn() == 13 || e.getColumn() == 17 || e.getColumn() == 18) {
                    Double priceOut = !addingTable.getValueAt(row, 6).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 6).toString())) : new Double("0");
                    Double hourOut = !addingTable.getValueAt(row, 9).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 9).toString())) : new Double("0");
                    Double priceIn = !addingTable.getValueAt(row, 12).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 12).toString())) : new Double("0");
                    Double hourIn = !addingTable.getValueAt(row, 13).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 13).toString())) : new Double("0");
                    Double arenda = !addingTable.getValueAt(row, 17).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 17).toString())) : new Double("0");
                    Double prorab = !addingTable.getValueAt(row, 18).toString().isEmpty() ? Double.valueOf(Double.valueOf(addingTable.getValueAt(row, 18).toString())) : new Double("0");
                    addingTable.setValueAt(priceOut * hourOut - priceIn * hourIn - arenda - prorab, row, 15);
                }
            }
        }

        );

        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.setRowCount(0);
                saveButton.setEnabled(false);
            }
        });
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MyUtils.isDouble(priceHoursOurField.getText()) && MyUtils.isDouble(priceHoursCustomerField.getText())) {
                    saveButton.setEnabled(true);
                    Date dtFrom = dateFrom.getCalendar().getTime();
                    Date dtTo = dateTo.getCalendar().getTime();
                    while (MyUtils.compareDate(dtTo, dtFrom)) {
                        model.addRow(new Object[]{(new SimpleDateFormat("dd.MM.yyyy")).format(dtFrom.getTime()), chInField.getText(), chOutField.getText(), tecNameCombo.getSelectedItem(), managerCombo.getSelectedItem(), customerCombo.getSelectedItem(), priceHoursCustomerField.getText(), "", "", "", "", performerCombo.getSelectedItem(), priceHoursOurField.getText(), "", "", "", objectCombo.getSelectedItem(), "", "", ""});
                        dtFrom.setTime(dtFrom.getTime() + 24 * 60 * 60 * 1000);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Цена может быть только числом, проверьте вводимые значения!!!", "Не верно введены значения.", JOptionPane.OK_OPTION);
                }
            }
        });

        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int response = JOptionPane.showOptionDialog(null, "Вы, действительно хотите удалить запись(си)?", "Удалить?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
                if (response == JOptionPane.YES_OPTION) {
                    while (addingTable.getSelectedRows().length > 0) {
                        ((DefaultTableModel) addingTable.getModel()).removeRow(addingTable.getSelectedRows()[0]);
                    }

                }

            }

        });

        addingTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

            }
        }
        );
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    File file = new File(VariableNames.PATH_TO_BD);
                    InputStream in = new FileInputStream(file);
                    HSSFWorkbook wb = new HSSFWorkbook(in);
                    ExcelExtractor extractor = new ExcelExtractor(wb);
                    extractor.setFormulasNotResults(false); // Считать формулы
                    HSSFSheet sheet = wb.getSheetAt(0);
                    HSSFCellStyle cellStyle = wb.createCellStyle();
                    cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
                    cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
                    cellStyle.setBorderRight(CellStyle.BORDER_THIN);
                    cellStyle.setBorderTop(CellStyle.BORDER_THIN);

                    for (int i = 0; i < addingTable.getRowCount(); i++) {
                        HSSFRow row = MyUtils.getFirstEmptyRow(sheet);
                        HSSFCell cel1D = row.createCell(0);
                        Calendar dateCal = Calendar.getInstance();
                        dateCal.set(Integer.valueOf(addingTable.getValueAt(i, 0).toString().substring(6, 10)), Integer.valueOf(addingTable.getValueAt(i, 0).toString().substring(3, 5)) - 1, Integer.valueOf(addingTable.getValueAt(i, 0).toString().substring(0, 2)));
                        cel1D.setCellValue(dateCal.getTime());
                        CellStyle dateCellStyle = wb.createCellStyle();
                        dateCellStyle.setDataFormat(wb.createDataFormat().getFormat("dd.MM.yyyy"));
                        dateCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
                        dateCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
                        dateCellStyle.setBorderRight(CellStyle.BORDER_THIN);
                        dateCellStyle.setBorderTop(CellStyle.BORDER_THIN);

                        cel1D.setCellStyle(dateCellStyle);
                        for (int j = 1; j < Constants.NAEM_НEADER.length; j++) {
                            HSSFCell cel1 = row.createCell(j);
                            if (j == 6 || j == 9 || j == 10 || j == 10 || j == 12 || j == 13 || j == 14 || j == 15 || j == 17 || j == 18) {
                                cel1.setCellType(Cell.CELL_TYPE_NUMERIC);
                                cel1.setCellValue(!(String.valueOf(addingTable.getValueAt(i, j))).isEmpty() ? Double.valueOf(addingTable.getValueAt(i, j).toString()) : new Double(0));

                            } else {
                                cel1.setCellValue(addingTable.getValueAt(i, j) != null ? addingTable.getValueAt(i, j).toString() : "");
                            }
                            cel1.setCellStyle(cellStyle);
                        }
                    }
                    FileOutputStream fileOut = new FileOutputStream(VariableNames.PATH_TO_BD);
                    wb.write(fileOut);
                    fileOut.close();
                    JOptionPane.showMessageDialog(null, "Изменение успешно сохранены", "Изменения сохранены", JOptionPane.OK_OPTION, new ImageIcon(ResourceManager.class.getResource("/images/success.png")));
                    MainDataBuilder.buildData();
                    Searcher.searchNaem();
                    Searcher.searchOur();
                    cleanTable();
                } catch (IOException e1) {
                    JOptionPane.showOptionDialog(null, "Нет доступа к базе данных. Она используется другой программой.\n Закройте все программы которые могут использовать ее (скорее всего это excel)", "данные не изменены", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
                }
            }
        });
    }

    private void cleanTable() {
        model.setRowCount(0);
        saveButton.setEnabled(false);
    }

    private void setWidth(JTable detailedTable) {
        // ORS-22
        int col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[0]);
        TableColumn column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[1]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(55);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[2]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(60);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[3]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(150);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[4]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[5]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(150);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[6]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(85);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[7]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(85);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[8]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[9]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(110);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[10]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(75);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[11]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(75);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[12]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[13]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[14]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(120);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[15]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[16]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);
        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[17]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);
        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[18]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
        column.setMinWidth(70);
    }

    public class NaemPanelDefaultTableModel extends DefaultTableModel {

        public NaemPanelDefaultTableModel(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == 10 || col == 14) {
                return false;
            } else {
                return true;
            }
        }
    }
}
